package com.ereport.controller.reportsController;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.ereport.dao.DailyActivityDAO;
import com.ereport.entity.DailyActivity;
import com.ereport.entity.Executives;

@Controller
@RequestMapping("/DailyCallReport")
public class DailyCallReportController {
	private static final int BYTES_DOWNLOAD = 1024;
	@RequestMapping(value="/open.do", method=RequestMethod.GET)
	public ModelAndView open(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		
		model.setViewName("/jsp/dailycallInReport.jsp");
		return model;
	}
	@RequestMapping(value="/getActivity.do")
	public ModelAndView getActivity(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		String ddate=req.getParameter("dailyDate");
		System.out.println("date is "+ddate);
		Date dat=null;
		if(!ddate.equals("")){
		DateFormat frmat=new SimpleDateFormat("dd-MM-yyyy");
		try {
			dat = frmat.parse(ddate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		}
		DailyActivityDAO dadao=new DailyActivityDAO();
		List<DailyActivity> dalist=null;
		try {
			dalist=dadao.getActivity(ex.getId(),new java.sql.Date(dat.getTime()));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		session.setAttribute("dalist",dalist);
		
		System.out.println("dalist object is "+dalist);
		model.setViewName("redirect:open.do");
		model.addObject("edate", ddate);
		model.addObject("fdate", null);
		model.addObject("tdate", null);
		model.addObject("mdate", null);
	return model;
	}
	
	@RequestMapping(value="/getwkActivity.do")
	public ModelAndView getwkActivity(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		
		String frdate=req.getParameter("week1Date");
		String todate=req.getParameter("week2Date");
		
		DailyActivityDAO dadao=new DailyActivityDAO();
		
		Date fdat=null;
		if(frdate.length()!=0 || frdate!=null){
			DateFormat frat=new SimpleDateFormat("dd-MM-yyyy");
			try {
				fdat = frat.parse(frdate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		Date tdat=null;
		if(todate.length()!=0 || todate!=null){
			DateFormat frt=new SimpleDateFormat("dd-MM-yyyy");
			try {
				tdat = frt.parse(todate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		
		List<DailyActivity> wklist=null;
		try {
			wklist=dadao.getWeekActivity(ex.getId(),new java.sql.Date(fdat.getTime()),new java.sql.Date(tdat.getTime()));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		session.setAttribute("wklist",wklist);
		
		
		model.setViewName("redirect:open.do");
		model.addObject("edate", null);
		
		model.addObject("mdate", null);
		model.addObject("fdate", frdate);
		model.addObject("tdate", todate);
		
		return model;
	}
	@RequestMapping(value="/getmnActivity.do")
	public ModelAndView getmnActivity(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		
		String month=req.getParameter("month");
		String year=req.getParameter("year");
		
		DailyActivityDAO dadao=new DailyActivityDAO();
		
		List<DailyActivity> mnlist=null;
		try {
			mnlist=dadao.getMnthActivity(ex.getId(),month,year);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		session.setAttribute("mnlist",mnlist);
		
		model.setViewName("redirect:open.do");
		model.addObject("edate", null);
		model.addObject("fdate", null);
		model.addObject("tdate", null);
		
		model.addObject("mdate", month);
		model.addObject("ydate", year);
		return model;
	}
	@RequestMapping(value="/getMnExcel.do")
	public ModelAndView getExcel(HttpServletRequest req,HttpServletResponse response, HttpSession session,ModelAndView model) throws IOException{
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		
		String month=req.getParameter("month");
		String year=req.getParameter("year");
		
		DailyActivityDAO dadao=new DailyActivityDAO();
		
		List<DailyActivity> mnlist=null;
		try {
			mnlist=dadao.getMnthActivity(ex.getId(),month,year);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String fileName = "MonthDailCallReport"+year+"-"+month+".xls";
		  response.setHeader("Content-Disposition", "inline; filename=" + fileName);
				
		session.setAttribute("mnlist",mnlist);
		model.setViewName("/jsp/excelMnDailyAct.jsp");
		
		return model;
	}
	@RequestMapping(value="/getWkExcel.do")
	public ModelAndView getWkExcel(HttpServletRequest req,HttpServletResponse response, HttpSession session,ModelAndView model) throws IOException{
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		
		String frdate=req.getParameter("week1Date");
		String todate=req.getParameter("week2Date");
		
		DailyActivityDAO dadao=new DailyActivityDAO();
		
		Date fdat=null;
		if(frdate.length()!=0 || frdate!=null){
			DateFormat frat=new SimpleDateFormat("dd-MM-yyyy");
			try {
				fdat = frat.parse(frdate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		Date tdat=null;
		if(todate.length()!=0 || todate!=null){
			DateFormat frt=new SimpleDateFormat("dd-MM-yyyy");
			try {
				tdat = frt.parse(todate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		
		List<DailyActivity> wklist=null;
		try {
			wklist=dadao.getWeekActivity(ex.getId(),new java.sql.Date(fdat.getTime()),new java.sql.Date(tdat.getTime()));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String fileName = "WeekDailCallReport"+frdate+"To"+todate+".xls";
		response.setHeader("Content-Disposition", "inline; filename=" + fileName);
		
		session.setAttribute("wklist",wklist);
		model.setViewName("/jsp/excelWkDailyAct.jsp");
		
		return model;
	}
	@RequestMapping(value="/getDayExcel.do")
	public ModelAndView getDayExcel(HttpServletRequest req,HttpServletResponse response, HttpSession session,ModelAndView model) throws IOException{
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		
		String ddate=req.getParameter("dailyDate");
		System.out.println("date is "+ddate);
		Date dat=null;
		if(!ddate.equals("")){
		DateFormat frmat=new SimpleDateFormat("dd-MM-yyyy");
		try {
			dat = frmat.parse(ddate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		}
		DailyActivityDAO dadao=new DailyActivityDAO();
		List<DailyActivity> dalist=null;
		try {
			dalist=dadao.getActivity(ex.getId(),new java.sql.Date(dat.getTime()));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		session.setAttribute("dalist",dalist);
		String fileName = "DayDailCallReport"+ddate+".xls";
		response.setHeader("Content-Disposition", "inline; filename=" + fileName);
		
		
		model.setViewName("/jsp/excelDayDailyAct.jsp");
		
		return model;
	}
}
